import api from '../../../api/index';
import Toast from '@vant/weapp/toast/toast';
Component({ 
     
    /** 
     * 组件的属性列表 
     * 用于组件自定义设置 
    */ 
    properties: {
			idvalue:{
				type: String,
				value:''
			},//切换视图值
			acId: {
				type: String,
				value:''
			},//切换视图值
		},
    /** 
     * 私有数据,组件的初始数据 
     * 可用于模版渲染 
     */ 
    data: {
				type: 1,
				// src: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/587c0045955843eeaa1978dcec4a0107newfile_VID_20240103_16104440.mp4',
				src: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/aZ4tZRWXnH3E947b96af0b1bc3f36b50614d1cea5c1a.mp4',
					vider: '',
				w: 0,
				h: 0,
				list: null,
				showoverlay: false,
				index1: 0,
				index2: 0,
				inputValue: '',
				level1: '',
				commentList: [],
				showComments: false,
				id: '',
				bgmusic: ''
    },
		observers: {
			'acId'(data){  // canvasUrl: 父组件传递过来的值
				console.log(data, this.data.idvalue,'acIdacIdacId')
				if (data === this.data.idvalue) {
					setTimeout(() => {
						if(this.data.bgmusic) {
							wx.playBackgroundAudio({
								dataUrl: this.data.bgmusic
							});
						}
					}, 1500)
					this.start()
				} else {
					this.stop()
					wx.stopBackgroundAudio()
				}
			}
		},
    /**
     * 组件的方法列表 
     * 更新属性和数据的方法与更新页面数据的方法类似 
    */ 
    methods: { 
			start() {
				var videoContext = wx.createVideoContext('videso' + this.data.idvalue, this)
        videoContext.play();
			},
			stop() {
				var videoContext = wx.createVideoContext('videso' + this.data.idvalue, this)
        videoContext.stop();
			},
			tapMethod(e) {
				this.setData({ type: e.currentTarget.dataset.type })
			},
			onUserClick(e) {
				console.log(e);
				var userid = e.currentTarget.dataset.userid
				wx.navigateTo({ url: `/community/pages/adduser/index?userid=${userid}` });
			},
			// 关注
			onFollowClick(e) {
				console.log(e);
				var userid = e.currentTarget.dataset.userid
				try {
					api.get(`/api/hxhealth/forum/author/follow/${userid}`).then((res) => {
						console.log(res);
						Toast.success('关注成功');
						this.getArticleDesc(this.data.id)
					});
				} catch (e) {
					console.error(e);
				}
			},
			// 已关注
			onUnFollowClick(e) {
				console.log(e);
				var userid = e.currentTarget.dataset.userid
				try {
					api.get(`/api/hxhealth/forum/author/unfollow/${userid}`).then((res) => {
						console.log(res);
						Toast.success('取消成功');
						this.getArticleDesc(this.data.id)
					});
				} catch (e) {
					console.error(e);
				}
			},
			createComment(e) {
				this.setData({
					showoverlay: true,
					level1: e.currentTarget.dataset.createcommenttype === '1' ? '' : e.currentTarget.dataset.id
				})
			},
			createComment2(e) {
				this.setData({
					showoverlay: true,
					level1: e.currentTarget.dataset.id
				})
				console.log(this.data.showoverlay)
			},
			onClickHide() {
				this.setData({
					showoverlay: false,
					showComments: false
				})
			},
			onclickzans(e) {
				var item = e.currentTarget.dataset.item
				if(item.zaned == 'false') {
					try {
						api.get(`/api/hxhealth/forum/article/zan/${item.id}`).then((res) => {
							console.log(res);
							Toast.success('点赞成功');
							// this.getauthorUser(this.data.userid) 
							this.getArticleDesc(this.data.id)
						});
					} catch (e) {
						console.error(e);
					}
				} else {
					try {
						api.get(`/api/hxhealth/forum/article/unzan/${item.id}`).then((res) => {
							console.log(res);
							Toast.success('取消成功');
							// this.getauthorUser(this.data.userid)
							this.getArticleDesc(this.data.id)
						});
					} catch (e) {
						console.error(e);
					}
				}
			},
			onInput() {
				// 获取输入框的高度
				const query = wx.createSelectorQuery();
				query.select('.input_1').boundingClientRect();
				query.selectViewport().scrollOffset();
				query.exec((res) => {
					const inputHeight = res[0].height;
					const scrollTop = res[1].scrollTop;
					const windowHeight = wx.getSystemInfoSync().windowHeight;
		
					// 计算输入框距离页面底部的距离
					const bottom = windowHeight - scrollTop - inputHeight;
		
					// 如果输入框被遮挡，则将页面滚动到输入框位置
					if (bottom < 0) {
						wx.pageScrollTo({
							scrollTop: scrollTop - bottom
						});
					}
				});
			},
		onChange(e) {
			this.setData({
				inputValue: e.detail
			})
		},
		confirmTap() {
			const value = this.data.inputValue;
			if(!value) {
				wx.showToast({
					title: '请输入评论内容',
					icon: 'error',
					duration: 2000
				})
				return
			}
			this.putcomment(value)
		},
		// 评论列表
		commentList (id) {
			const actions = {
				data: {
					articleid: id
				},
				head: {
					firstresult: this.data.pageNum,
					maxresults: this.data.pageSize,
					bodytype: 'flat',
				}
			};
			try {
				api.post('/api/hxhealth/forum/comment/list', actions).then((res) => {
					res.forEach(item => {
						item.showChild = false
						item.childComments = []
					})
					this.setData({
						commentList: res,
						level1: '',
						showComments: true
					})
				});
			} catch (e) {
				console.error(e);
			}
		},
		// 查询二级评论
		onHuifuClick(e) {
			var id = e.currentTarget.dataset.id
			var index = e.currentTarget.dataset.index
			const actions = {
				data: {
					level1: id,
					articleid: this.data.id
				},
				head: {
					firstresult: this.data.pageNum,
					maxresults: this.data.pageSize,
					bodytype: 'flat',
				}
			};
			try {
				api.post('/api/hxhealth/forum/comment/list', actions).then((res) => {
					const list = this.data.commentList
					list[index].childComments = res
					list[index].showChild = true
					this.setData({
						commentList: list
					})
				});
			} catch (e) {
				console.error(e);
			}
		},
		openCommentsInfo() {
			this.commentList(this.data.id)
		},
		// 一级评论
		putcomment(value) {
			const actions = {
				data: {
					level1: this.data.level1,
					articleid: this.data.id,
					comment: value
				},
				head: {
					firstresult: this.data.pageNum,
					maxresults: this.data.pageSize,
					bodytype: 'flat',
				}
			};
			try {
				api.post('/api/hxhealth/forum/comment/save', actions).then((res) => {
					console.log(res);
					this.setData({
						inputValue: null,
						showoverlay: false,
					})
					Toast('评论成功~');
					this.getArticleDesc(this.data.id)
					this.commentList(this.data.id)
				});
			} catch (e) {
				console.error(e);
			}
		},
		// 获取关注的文章
		getArticleDesc(id) {
			try {
				api.get(`/api/hxhealth/forum/article/load/${id}`).then((res) => {
					console.log(res);
					res[0].images = res[0].media?JSON.parse(res[0].media).images:'',
					res[0].videos= res[0].media?JSON.parse(res[0].media).videos[0].file:'',
					res[0].zanusers = res[0].zanusers?JSON.parse(res[0].zanusers):''
					res[0].name = res[0].name?res[0].name:''
					this.setData({
						list: res[0],
						userid: res[0].userid
					})
					if(res[0].media) {
						let media = JSON.parse(res[0].media)
						if(media.bgmusic) {
							console.log('-------jinlailemuisc------------');
							res[0].bgmusic = media.bgmusic
							this.setData({
								bgmusic: media.bgmusic.file
							})
						}
					}
					console.log(res[0]);
					Toast.clear();
				});
			} catch (e) {
				console.error(e);
			}
		},
		goSearch() {
			wx.navigateTo({ url: `/community/pages/search/index` });
		},
		goShop() {
			wx.navigateTo({ url: `/shop/pages/home/index` });
		},
		// 点赞或者取消点赞
		onZanClick(e) {
			if(e.currentTarget.dataset.zantype === '1') {
				this.setData({
					index1: e.currentTarget.dataset.index1
				})
			} else if (e.currentTarget.dataset.zantype === '2') {
				this.setData({
					index1: e.currentTarget.dataset.index1,
					index2: e.currentTarget.dataset.ids
				})
			}
			var zand = e.currentTarget.dataset.zaned
			var id = e.currentTarget.dataset.id
			if(zand == 'false') {
				this.zan(id, e.currentTarget.dataset.zantype)
			} else {
				this.unzan(id, e.currentTarget.dataset.zantype)
			}
		},
		zan(id, zantype) {
			try {
				api.get(`/api/hxhealth/forum/comment/zan/${id}`).then((res) => {
					console.log(res);
					if(zantype === '1') {
						const list = this.data.commentList
						let num = Number(list[this.data.index1].zans)
						list[this.data.index1].zans = num + 1
						list[this.data.index1].zaned = 'true'
						this.setData({
							commentList: list
						})
					} else {
						const list = this.data.commentList
						let num = Number(list[this.data.index1].childComments[this.data.index2].zans)
						list[this.data.index1].childComments[this.data.index2].zans = num + 1
						list[this.data.index1].childComments[this.data.index2].zaned = 'true'
						this.setData({
							commentList: list
						})
					}
					Toast.clear();
				});
			} catch (e) {
				console.error(e);
			}
		},
		unzan(id, zantype) {
			try {
				api.get(`/api/hxhealth/forum/comment/unzan/${id}`).then((res) => {
					if(zantype === '1') {
						const list = this.data.commentList
						let num = Number(list[this.data.index1].zans)
						list[this.data.index1].zans = num - 1
						list[this.data.index1].zaned = 'false'
						this.setData({
							commentList: list
						})
					} else {
						const list = this.data.commentList
						let num = Number(list[this.data.index1].childComments[this.data.index2].zans)
						list[this.data.index1].childComments[this.data.index2].zans = num - 1
						list[this.data.index1].childComments[this.data.index2].zaned = 'false'
						this.setData({
							commentList: list
						})
					}
					Toast.clear();
				});
			} catch (e) {
				console.error(e);
			}
		},
		// 收藏文章
		onclickcollects(e) {
			console.log( e.currentTarget.dataset);
			var collected = e.currentTarget.dataset.item.collected
			var id = e.currentTarget.dataset.item.id
			console.log(collected);
			if(collected == 'false') {
				console.log('收藏');
				this.cang(id)
			} else {
				console.log('取消收藏');
				this.uncang(id)
			}
		},
		cang (id) {
			try {
				api.get(`/api/hxhealth/forum/article/collect/${id}`).then((res) => {
					console.log(res);
					this.getArticleDesc(this.data.id)
					Toast.clear();
				});
			} catch (e) {
				console.error(e);
			}
		},
		uncang (id) {
			try {
				api.get(`/api/hxhealth/forum/article/uncollect/${id}`).then((res) => {
					console.log(res);
					this.getArticleDesc(this.data.id)
					Toast.clear();
				});
			} catch (e) {
				console.error(e);
			}
		}
    },
		lifetimes: {
			ready: function() {
				// 在组件实例进入页面节点树时执行
				console.log(this.data.idvalue)
				this.setData({
					id: this.data.idvalue
				})
				this.getArticleDesc(this.data.idvalue)
			},
			detached: function() {
				// 在组件实例被从页面节点树移除时执行
			},
		},
  })